﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using RBS.Common;
 

namespace RBS.Admin.Controllers
{
    public class BaseController : Controller
    { 
         /// <summary>
         /// 管理员登录
         /// </summary>
         /// <returns></returns>
        public bool IsAdminLogin()
        {
            //如果Session为Null
            if (Session[RBSKeys.SESSION_ADMIN_INFO] != null)
            {
                return true;
            }
            else
            {
                //检查Cookies
                string adminname = Utils.GetCookie("AdminName", "DTcms");
                string adminpwd = Utils.GetCookie("AdminPwd", "DTcms");
                if (adminname != "" && adminpwd != "")
                {
                    Core.BLL.manager bll = new Core.BLL.manager();
                    Core.Model.manager model = bll.GetModel(adminname, adminpwd);
                    if (model != null)
                    {
                        Session[RBSKeys.SESSION_ADMIN_INFO] = model;
                        return true;
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// 取得管理员信息
        /// </summary>
        public RBS.Core.Model.manager GetAdminInfo()
        {
            if (IsAdminLogin())
            {
                RBS.Core.Model.manager model = Session[RBSKeys.SESSION_ADMIN_INFO] as Core.Model.manager;
                if (model != null)
                {
                    return model;
                }
            }
            return null;
        }
         /// <summary>
        /// 写入管理日志
        /// </summary>
        /// <param name="action_type"></param>
        /// <param name="remark"></param>
        /// <returns></returns>
        public bool AddAdminLog(string action_type, string remark)
        {

            Core.Model.manager model = GetAdminInfo();
            int newId = new Core.BLL.manager_log().Add(model.id, model.user_name, action_type, remark);
                if (newId > 0)
                {
                    return true;
                }
            return false; 
        }

        public void ChkAdminLevel(string nav_name, string action_type)
        { 

            Core.Model.manager model = GetAdminInfo();
            Core.BLL.manager_role bll = new Core.BLL.manager_role();
            bool result = bll.Exists(model.role_id, nav_name, action_type);
            if (!result)
            {
                string msgbox = "parent.jsdialog(\"错误提示\", \"您没有管理该页面的权限，请勿非法进入！\", \"back\", \"Error\")";
                Response.Write("<script type=\"text/javascript\">" + msgbox + "</script>");
                Response.End();
            }
        }
        #region 返回角色类型名称=========================
        public string GetTypeName(int role_type)
        {
            string str = "";
            switch (role_type)
            {
                case 1:
                    str = "超级用户";
                    break;
                default:
                    str = "系统用户";
                    break;
            }
            return str;
        }
        #endregion
        #region 绑定导航菜单=============================

        protected List<SelectListItem> TreeBind(string nav_type, int id = 0)
        {
            List<SelectListItem> selectListItems = new List<SelectListItem>();
            RBS.Core.BLL.SysNavigation bll = new RBS.Core.BLL.SysNavigation();
            DataTable dt = bll.GetList(0, nav_type);
            selectListItems.Clear();
            selectListItems.Add(new SelectListItem { Text = "无父级导航", Value = "0" });
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                int ClassLayer = int.Parse(dr["class_layer"].ToString());
                string Title = dr["title"].ToString().Trim();

                if (ClassLayer == 1)
                {
                    if (Id == id.ToString())
                    {
                        selectListItems.Add(new SelectListItem { Text = Title, Value = Id, Selected = true });
                    }
                    else
                    {
                        selectListItems.Add(new SelectListItem { Text = Title, Value = Id });
                    }

                }
                else
                {
                    Title = "├ " + Title;
                    Title = Utils.StringOfChar(ClassLayer - 1, "　") + Title;
                    if (Id == id.ToString())
                    {
                        selectListItems.Add(new SelectListItem { Text = Title, Value = Id, Selected = true });
                    }
                    else
                    {
                        selectListItems.Add(new SelectListItem { Text = Title, Value = Id });
                    }
                }
            }
            return selectListItems;
        }
        #endregion
        #region 角色类型=================================

        protected List<SelectListItem> RoleBind(int roleId)
        {
            DataTable dt = new RBS.Core.BLL.manager_role().GetList("").Tables[0]; 
            var selectListItems = new List<SelectListItem>(); 
            selectListItems.Clear();
           // selectListItems.Add(new SelectListItem { Text = "请选择类型", Value = "0" });
            selectListItems.AddRange(from DataRow dr in dt.Rows
                                     let Id = dr["id"].ToString()
                                     let roleType = int.Parse(dr["role_type"].ToString())
                                     let title = dr["role_name"].ToString().Trim()
                                     select Id == roleId.ToString() ? new SelectListItem { Text = title, Value = Id, Selected = true } : new SelectListItem { Text = title, Value = Id });
            return selectListItems;
        }

        

        #endregion

    }
}
